const { HttpException } = require('../core/http-exception')
const catchError = async (ctx, next) => {
    try {
        await next()
    } catch (err) {
        // 开发环境
        const isHttpException = err instanceof HttpException
        const ENV = process.env.NODE_ENV == 'dev';
        if(ENV&&!isHttpException){
            throw err
        }
        
        if (isHttpException) {
            ctx.body = {
                msg: err.msg,
                errorCode: err.errorCode,
                request: `${ctx.method} ${ctx.path}`
            }

            ctx.status = err.code
        }
    }
}

module.exports = catchError